import _ from 'lodash';
// import moment from 'moment';

import * as types from '@/store/mutation-types';
import {
  sendgetFood, sendgetFoodClosed,
} from '@/request/Menu';
// import { get } from '@/common/utils';
// import router from '@/router';

export default {
  // 发送请求
  // eslint-disable-next-line
  async getFood({ commit }, { id, page, limit, keyword }) {
    commit(types.SHOW_LOADING);
    try {
      // eslint-disable-next-line

      const result = await sendgetFood({
        id, page, limit, keyword,
      }); // 发送请求
      // console.log('getFood==>', result);

      commit(types.SAVE_FOOD_LIST, result); // 放到vuex里面

      //
    } catch (error) {
      // 原来log出起来的500，经过包装就是CustomError的新对象s
      commit(types.SHOW_ALERT, { message: error.message });
    } finally {
      // 用try catch 主要是为了用finally 最终能将loading去掉
      commit(types.HIDE_LOADING);
    }
  },

  async getFoodClosed({ commit, dispatch }, {
    idclose, available, id, page, limit, keyword,
  }) {
    commit(types.SHOW_LOADING);
    try {
      // eslint-disable-next-line
      const result = await sendgetFoodClosed({ id: idclose, available }); // 发送请求
      // console.log('getFoodClosed==>', result);
      //
    } catch (error) {
      commit(types.SHOW_ALERT, { message: error.message });// 原来log出起来的500，经过包装就是CustomError的新对象
    } finally {
      commit(types.HIDE_LOADING);
      dispatch('getFood', {
        id, page, limit, keyword,
      }); //* */ 重新传值
    }
  },

  modifyShopName({ commit }, newList) {
    console.log('newList: ', newList);
    commit(types.PINYIN_SEARCH, newList); // 放到vuex里面
  },

  clickShopName({ commit, rootState }) {
    //* */ 设置拼音搜索渲染数组的初始值（可以跨文件）
    const shopListName = [];
    _.forEach(rootState.restaurant.list, (element) => {
      shopListName.push({ name: element.name['zh-CN'], id: _.get(element, '_id') });
    });

    commit(types.PINYIN_SEARCH, shopListName); // 放到vuex里面
    console.log('shopListName: ', shopListName);
  },

};
